package com.xinqi.modules.user.auth.controller.socket;

import com.alibaba.nacos.common.utils.UuidUtils;
import com.xinqi.common.base.response.R;
import com.xinqi.common.core.constant.CacheConstants;
import com.xinqi.common.core.utils.Md5Utils;
import com.xinqi.common.log.asynclog.annotation.AsyncLog;
import com.xinqi.common.log.asynclog.enums.AsyncOperaType;
import com.xinqi.common.log.asynclog.enums.AsyncType;
import com.xinqi.common.redis.utils.RedisUtils;
import com.xinqi.modules.user.auth.form.ScanBody;
import com.xinqi.modules.user.auth.service.SysAppLoginService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;

/**
 * Socket扫码登录控制器
 *
 * @version V1.0
 * @author: dzy
 * @date: 2023-03-09 11:01
 */
@Slf4j
@RestController
@RequestMapping("/socket")
@RequiredArgsConstructor
public class SocketController {

    private final SysAppLoginService appLoginService;

    /**
     * 获取扫码信息
     *
     * @param
     * @author: dgyu
     * @date: 2023-03-11 16:08
     * @Return
     * @Description:
     */
    @PostMapping("/scanCode")
    public R<String> scanCode() {
        String signStr = Md5Utils.hash(UuidUtils.generateUuid()) + "c9b2533069ebfa67";
        RedisUtils.setCacheObject(CacheConstants.SOCKET_LOGIN_SCAN_KEY + signStr, signStr, Duration.ofSeconds(58));
        return R.ok("获取成功", signStr);
    }

    /**
     * 扫码登录
     *
     * @author: dzy
     * @date: 2023-03-11 16:08
     * @Return
     * @Description:
     */
    @AsyncLog(title = "扫码登录", asyncType = AsyncType.LOGIN, operatorType = AsyncOperaType.USER_LOGIN)
    @PostMapping("/scanLogin")
    public R<Void> scanLogin(@RequestBody ScanBody scanBody) {
        return appLoginService.createMessageInfo(scanBody.getSignCode());
    }

}
